﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using FXC.SL.CS.Util;
using FXC.SL.FXCenterService;
using FXC.SL.Interface;
using Telerik.Windows.Controls.Charting;
using System.Linq;

namespace FXC.SL.UserControls.FXCChart
{
    public partial class FXProductChart : UserControl
    {
        private SeriesMapping smMain = new SeriesMapping();
        public string ProductName { get; set; }
        FXCenterService.FXCServiceClient proxy = FXCServiceClientManager.CreateProxy();

        public void GetDataFromService()
        {
            proxy.GetFXCDataCompleted += (o, e) =>
            {
                if (e.Error == null)
                {
                    LoadData(e.Result.Take(30).ToList());
                }
                else
                {
                }
            };
            proxy.GetFXCDataAsync(ProductName, "30M");
        }

        public FXProductChart()
        {
            InitializeComponent();
            rcProduct.DefaultView.ChartArea.ItemToolTipOpening += ChartArea_ItemToolTipOpening;
        }

        private ChartType _currentChartType;
        public ChartType CurrentChartType
        {
            get
            {
                return _currentChartType;
            }
            set
            {
                //if (_currentChartType == value)
                //    return;
                //if (rcProduct.SeriesMappings.Contains(smMain))
                    //rcProduct.SeriesMappings.Remove(smMain);
                switch (value)
                {
                    case ChartType.CandleStick:
                        smMain.SeriesDefinition = new CandleStickSeriesDefinition();
                        break;
                    case ChartType.Line:
                        smMain.SeriesDefinition = new SplineSeriesDefinition();
                        break;
                    case ChartType.Stick:
                        smMain.SeriesDefinition = new StickSeriesDefinition();
                        break;
                    default:
                        smMain.SeriesDefinition = new CandleStickSeriesDefinition();
                        break;
                }
                //_currentChartType = value;

                if (OnChartTypeChanged != null)
                    OnChartTypeChanged(this, new ChartTypeChangeEventArgs { chartType = CurrentChartType });
                rcProduct.Rebind();
                //FillMainSeries(smMain);
            }
        }

        #region IChart Members

        public void ChangeFXType()
        {
            throw new NotImplementedException();
        }

        public void ChangeZoomBig()
        {
            throw new NotImplementedException();
        }

        public void ChangeZoomSmall()
        {
            throw new NotImplementedException();
        }

        public void Refresh()
        {
            throw new NotImplementedException();
        }

        public void ChangeBackgourd()
        {
            throw new NotImplementedException();
        }

        public void ChangeShowVolume()
        {
            throw new NotImplementedException();
        }

        public event EventHandler OnFxTypeChanged;

        public event EventHandler<ChartTypeChangeEventArgs> OnChartTypeChanged;

        public event EventHandler OnBackgourdChanged;

        public event EventHandler OnShowVolumeChanged;

        public ChartType ChartType
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        #endregion

        private void ChartArea_ItemToolTipOpening(ItemToolTip2D tooltip, ItemToolTipEventArgs e)
        {
            if (e.DataPoint.Open != 0)
            {
                tooltip.Content = string.Format(" Day: {0} \r\n Open: {1}\r\n High: {2}\r\n Low: {3}\r\n Close: {4}",
                                                DateTime.FromOADate(e.DataPoint.XValue), e.DataPoint.Open,
                                                e.DataPoint.High, e.DataPoint.Low, e.DataPoint.Close);
            }
            else
            {
                tooltip.Content = string.Format(" Day: {0} \r\n Volume: {1}", DateTime.FromOADate(e.DataPoint.XValue),
                                                e.DataPoint.YValue);
            }
        }

        public void LoadData(List<FXData> list)
        {
            rcProduct.DefaultView.ChartLegend.Visibility = Visibility.Collapsed;
            rcProduct.DefaultView.ChartArea.LabelFormatBehavior = LabelFormatBehavior.None;
            rcProduct.DefaultView.ChartArea.AxisY.DefaultLabelFormat = "N5";

            smMain = new SeriesMapping();

            CurrentChartType = ChartType.Line;

            rcProduct.DefaultView.ChartArea.AxisX.IsDateTime = true;
            rcProduct.DefaultView.ChartArea.AxisX.DefaultLabelFormat = "yyyy/MM/dd HH:mm";
            rcProduct.DefaultView.ChartArea.AxisX.MinValue = list[0].ProcessDateTime.ToOADate();
            rcProduct.DefaultView.ChartArea.AxisX.Step = DateTime.Now.ToOADate() -
                                                         DateTime.Now.AddMinutes(-30).ToOADate();


            rcProduct.DefaultView.ChartArea.AxisX.LabelStep = 3;
            //rcProduct.DefaultView.ChartArea.AxisX.LabelRotationAngle = 90;


            //加第二个Series
            var volumnMapping = new SeriesMapping();
            volumnMapping.SeriesDefinition = new BarSeriesDefinition();
            volumnMapping.SeriesDefinition.ShowItemToolTips = true;
            volumnMapping.SeriesDefinition.ShowItemLabels = false;
            volumnMapping.SeriesDefinition.Appearance.Fill = new SolidColorBrush(Color.FromArgb(128, 0, 128, 255));
            volumnMapping.SeriesDefinition.Appearance.Stroke = new SolidColorBrush(Color.FromArgb(255, 0, 128, 255));
            volumnMapping.SeriesDefinition.Appearance.StrokeThickness = 1;
            volumnMapping.SeriesDefinition.AxisName = "Volumn";
            volumnMapping.ItemMappings.Add(new ItemMapping("ProcessDateTime", DataPointMember.XValue));
            volumnMapping.ItemMappings.Add(new ItemMapping("Volume", DataPointMember.YValue));

            rcProduct.SeriesMappings.Add(volumnMapping);


            var volumeAxis = new AxisY();
            volumeAxis.AxisName = "Volumn";
            volumeAxis.AutoRange = false;
            volumeAxis.MinValue = 0;
            volumeAxis.MaxValue = 30000;
            volumeAxis.Step = 100;
            volumeAxis.Visibility = Visibility.Collapsed;
            rcProduct.DefaultView.ChartArea.AdditionalYAxes.Add(volumeAxis);

            rcProduct.ItemsSource = list;
        }

        private void FillMainSeries(SeriesMapping smMain)
        {
            smMain.SeriesDefinition.ShowItemToolTips = true;

            var imHigh = new ItemMapping("HighPrice", DataPointMember.High);
            var imLow = new ItemMapping("LowPrice", DataPointMember.Low);
            var imOpen = new ItemMapping("OpenPrice", DataPointMember.Open);
            var imClose = new ItemMapping("ClosePrice", DataPointMember.Close);
            var imXDate = new ItemMapping("ProcessDateTime", DataPointMember.XValue);
            var imYDate = new ItemMapping("ClosePrice", DataPointMember.YValue);

            smMain.ItemMappings.Add(imHigh);
            smMain.ItemMappings.Add(imLow);
            smMain.ItemMappings.Add(imOpen);
            smMain.ItemMappings.Add(imClose);
            smMain.ItemMappings.Add(imXDate);
            smMain.ItemMappings.Add(imYDate);

            rcProduct.SeriesMappings.Add(smMain);
        }

        #region IChart Members


        public void ChangeChartType(ChartType chartType)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}